package org.nViron.nvRender;

//~--- non-JDK imports --------------------------------------------------------

import org.nViron.nvCommon.nvPosition;
import org.nViron.nvSim.nvSimulator;

/**
 * Axis aligned bounding box, used for frustum culling
 * @author Roland Haring <br>
 * based on code by <a href="http://www.lighthouse3d.com/opengl/viewfrustum/">Lighthouse3d</a> <p>
 * <script type="text/javascript" src="http://www.ohloh.net/p/376434/widgets/project_basic_stats.js"></script>
 */
public class nvAABox {
    public nvVec3 corner = new nvVec3();
    public float  x, y, z;

    public nvAABox() {
        x = 1.0f;
        y = 1.0f;
        z = 1.0f;
    }

    public nvAABox(nvPosition pos1, nvPosition pos2) {
        setBox(pos1, pos2);
    }

    public nvAABox(nvVec3 corner, float x, float y, float z) {
        setBox(corner, x, y, z);
    }

    public nvVec3 getVertexN(nvVec3 normal) {
        nvVec3 res = new nvVec3(corner);

        if (normal.x < 0) {
            res.x += x;
        }

        if (normal.y < 0) {
            res.y += y;
        }

        if (normal.z < 0) {
            res.z += z;
        }

        return (res);
    }

    public nvVec3 getVertexP(nvVec3 normal) {
        nvVec3 res = new nvVec3(corner);

        if (normal.x > 0) {
            res.x += x;
        }

        if (normal.y > 0) {
            res.y += y;
        }

        if (normal.z > 0) {
            res.z += z;
        }

        return res;
    }

    private void setBox(nvPosition pos1, nvPosition pos2) {
        nvVec3 c1 = nvSimulator.getInstance().getProjection().geoToViz(pos1);
        nvVec3 c2 = nvSimulator.getInstance().getProjection().geoToViz(pos2);

        setBox(c1, c2.x - c1.x, c2.y - c1.y, c2.z - c1.z);
    }

    public void setBox(nvVec3 corner, float x, float y, float z) {
        this.corner.copy(corner);

        if (x < 0.0) {
            x             = -x;
            this.corner.x -= x;
        }

        if (y < 0.0) {
            y             = -y;
            this.corner.y -= y;
        }

        if (z < 0.0) {
            z             = -z;
            this.corner.z -= z;
        }

        this.x = x;
        this.y = y;
        this.z = z;
    }
}



